package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.LuntaiMessage;
import com.ruoyi.system.domain.LuntaiPolicyExtensionRequest;
import com.ruoyi.system.domain.dto.LuntaiRescueApplicationDto;
import com.ruoyi.system.mapper.LuntaiMessageMapper;
import com.ruoyi.system.mapper.LuntaiOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LuntaiRescueApplicationMapper;
import com.ruoyi.system.domain.LuntaiRescueApplication;
import com.ruoyi.system.service.ILuntaiRescueApplicationService;

/**
 * 用户救援申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class LuntaiRescueApplicationServiceImpl implements ILuntaiRescueApplicationService {
    @Autowired
    private LuntaiRescueApplicationMapper luntaiRescueApplicationMapper;

    @Autowired
    private LuntaiMessageMapper luntaiMessageMapper;

    @Autowired
    private LuntaiOrderMapper luntaiOrderMapper;

    /**
     * 查询用户救援申请
     *
     * @param id 用户救援申请主键
     * @return 用户救援申请
     */
    @Override
    public LuntaiRescueApplication selectLuntaiRescueApplicationById(Integer id) {
        return luntaiRescueApplicationMapper.selectLuntaiRescueApplicationById(id);
    }

    /**
     * 查询用户救援申请列表
     *
     * @param luntaiRescueApplication 用户救援申请
     * @return 用户救援申请
     */
    @Override
    public List<LuntaiRescueApplication> selectLuntaiRescueApplicationList(LuntaiRescueApplication luntaiRescueApplication) {
        return luntaiRescueApplicationMapper.selectLuntaiRescueApplicationList(luntaiRescueApplication);
    }
    @Override
    public List<LuntaiRescueApplicationDto> selectLuntaiRescueApplicationList2(LuntaiRescueApplicationDto luntaiRescueApplication) {
        return luntaiRescueApplicationMapper.selectLuntaiRescueApplicationList2(luntaiRescueApplication);
    }
    /**
     * 新增用户救援申请
     *
     * @param luntaiRescueApplication 用户救援申请
     * @return 结果
     */
    @Override
    public int insertLuntaiRescueApplication(LuntaiRescueApplication luntaiRescueApplication) {
        luntaiRescueApplication.setCreateTime(DateUtils.getNowDate());
        return luntaiRescueApplicationMapper.insertLuntaiRescueApplication(luntaiRescueApplication);
    }

    /**
     * 修改用户救援申请
     *
     * @param luntaiRescueApplication 用户救援申请
     * @return 结果
     */
    @Override
    public int updateLuntaiRescueApplication(LuntaiRescueApplication luntaiRescueApplication) {
        Integer id = luntaiRescueApplication.getId();
        LuntaiRescueApplication application = luntaiRescueApplicationMapper.selectLuntaiRescueApplicationById(id);
        Integer status = luntaiRescueApplication.getStatus();
        if (status != null && status != 0) {
            sendMessage(luntaiRescueApplication, application);
            if (luntaiRescueApplication.getStatus() > 2) {
                luntaiOrderMapper.cancelOrder(luntaiRescueApplication.getId(), 3, Math.toIntExact(SecurityUtils.getUserId()));
            } else if (luntaiRescueApplication.getStatus() == 2) {
                updateOrderStatus(application);
            }
        }
        return luntaiRescueApplicationMapper.updateLuntaiRescueApplication(luntaiRescueApplication);
    }

    public void updateOrderStatus(LuntaiRescueApplication application) {
        Integer requestId = application.getId();
        luntaiOrderMapper.editOrder(requestId,3, BigDecimal.valueOf(0),2, Math.toIntExact(SecurityUtils.getUserId()));
    }

    public void sendMessage(LuntaiRescueApplication luntaiRescueApplication, LuntaiRescueApplication request) {
        LuntaiMessage luntaiMessage = new LuntaiMessage();
        luntaiMessage.setType(1);
        luntaiMessage.setTitle("您发起的救援申请管理员已处理");
        String result = "";
        // 假设 getCreateTime() 返回的是 Date 类型
        Date createTime = request.getCreateTime();
        // 创建 SimpleDateFormat 实例，设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        // 格式化时间
        String formattedTime = sdf.format(createTime);
        switch (luntaiRescueApplication.getStatus()) {
            case 1:
                result = "已接单";
                break;
            case 2:
                result = "已完成";
                break;
            case 3:
                result = "已取消";
                break;
            case 4:
                result = "已拒绝";
                break;
        }
        luntaiMessage.setInfo("您于" + formattedTime + "提交的救援申请管理员已处理，当前结果：" + result);
        luntaiMessage.setUserId(request.getUseId());
        luntaiMessage.setStoreId(request.getStoreId());
        luntaiMessage.setIsread(0);
        luntaiMessage.setIsdelete(0);
        luntaiMessageMapper.insertLuntaiMessage(luntaiMessage);
    }

    /**
     * 批量删除用户救援申请
     *
     * @param ids 需要删除的用户救援申请主键
     * @return 结果
     */
    @Override
    public int deleteLuntaiRescueApplicationByIds(Integer[] ids) {
        return luntaiRescueApplicationMapper.deleteLuntaiRescueApplicationByIds(ids);
    }

    /**
     * 删除用户救援申请信息
     *
     * @param id 用户救援申请主键
     * @return 结果
     */
    @Override
    public int deleteLuntaiRescueApplicationById(Integer id) {
        return luntaiRescueApplicationMapper.deleteLuntaiRescueApplicationById(id);
    }


}
